import { api } from "@/global/utils/api";
import { useI18n } from "vue-i18n";
import useApiPromiseHandler from "./api-promise-handler";

export default function useArticleModifier(onModifySuccess, onModifyFaild) {
  const { t } = useI18n();

  /**
   * 发布文章
   * @param article {
   *    title: '文章标题',
   *    content: '文章内容',
   *    tags: ['文章tag'...]
   * }
   */
  const handlePostArticle = (article) => {
    useApiPromiseHandler(t,
      api.post('/articles/create', article),
      onModifySuccess,
      onModifyFaild
    )
  }

  const handleDeleteArticle = (articleId) => {
    useApiPromiseHandler(t,
      api.get(`/articles/delete/${articleId}`),
      onModifySuccess, onModifyFaild
    );
  }

  const handleUpdateArticle = (article) => {
    useApiPromiseHandler(t,
      api.post(`/articles/update`, article),
      onModifySuccess, onModifyFaild
    );
  }

  const handleLikeArticle = (article) => {
    useApiPromiseHandler(t,
      api.get(`/articles/like/${article.id}`),
      () => {article.doseILikedIt = true; article.likeCount++; console.log(article)}, onModifyFaild
    );
  }

  const handleUnlikeArticle = (article) => {
    useApiPromiseHandler(t,
      api.get(`/articles/unlike/${article.id}`),
      () => {article.doseILikedIt = false; article.likeCount--;}, onModifyFaild
    );
  }
  return { handlePostArticle, handleDeleteArticle, handleUpdateArticle, handleLikeArticle, handleUnlikeArticle };
}